package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.domain.Review;
import com.example.domain.model.MyPage;
import com.example.domain.model.ReviewDTO;
import com.example.service.ReviewService;
import com.example.shiro.AccountProfile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "评价管理")
@RestController
@RequestMapping("/review")
public class ReviewController {
    @Autowired
    private ReviewService reviewService;

    @ApiOperation(value = "历史评价列表")
    @PostMapping("/myList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页索引", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示记录数", defaultValue = "20")
    })
    public R<IPage<ReviewDTO>> getMyList(@RequestBody MyPage myPage){
        //获取当前用户信息
        AccountProfile account = (AccountProfile) SecurityUtils.getSubject().getPrincipal();

        Page<ReviewDTO> page = new Page(myPage.getCurrent(),myPage.getSize());
        return R.success(reviewService.getMyList(account.getId(),page));
    }
    @ApiOperation(value = "评价列表")
    @PostMapping("/list/{movieId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页索引", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示记录数", defaultValue = "20")
    })
    public R<IPage<ReviewDTO>> getList(@PathVariable String movieId,@RequestBody MyPage myPage){

        Page<ReviewDTO> page = new Page(myPage.getCurrent(),myPage.getSize());

        return R.success(reviewService.getList(movieId,page));
    }
    @ApiOperation(value = "全部评价列表")
    @PostMapping("/allList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页索引", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示记录数", defaultValue = "20")
    })
    public R<IPage<ReviewDTO>> getAllList(@RequestBody MyPage myPage){

        Page<ReviewDTO> page = new Page(myPage.getCurrent(),myPage.getSize());

        return R.success(reviewService.getAllList(page));
    }


    @ApiOperation(value = "查看评价")
    @GetMapping("/{id}")
    public R<Review> getInfo(@PathVariable String id){
        //获取当前用户信息
        AccountProfile account = (AccountProfile) SecurityUtils.getSubject().getPrincipal();

        return R.success(reviewService.getById(id));
    }


    @ApiOperation(value = "新增评价")
    @PostMapping
    public R add(@RequestBody Review review){
        //获取当前用户信息
        AccountProfile account = (AccountProfile) SecurityUtils.getSubject().getPrincipal();

        return reviewService.save(review,account.getId()) ? R.success() : R.error();
    }
    //    @ApiOperation(value = "更新评价")
//    @PutMapping("/{id}")
    public R update(@PathVariable String id,@RequestBody Review review){
        return reviewService.update(id,review) ? R.success() : R.error();
    }

    @ApiOperation(value = "删除评价")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable String id){
        return reviewService.delete(id) ? R.success() : R.error();
    }
}
